package com.sicpa.web.controller.system;

import com.sicpa.common.annotation.Log;
import com.sicpa.common.core.controller.BaseController;
import com.sicpa.common.core.domain.AjaxResult;
import com.sicpa.common.core.domain.entity.AntiCounterfeitingUpdateContract;
import com.sicpa.common.core.domain.entity.SysUser;
import com.sicpa.common.core.page.TableDataInfo;
import com.sicpa.common.enums.BusinessType;
import com.sicpa.common.enums.RoleEnum;
import com.sicpa.system.service.IAntiCounterfeitingRegisteredOfficeService;
import com.sicpa.system.service.IAntiCounterfeitingUpdateContractService;
import com.sicpa.system.service.ICheckUserPowerService;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * AntiCounterfeitingUpdateContractController
 * @author lwf
 */
@RestController
@RequestMapping("/office/contacts")
public class AntiCounterfeitingUpdateContractController extends BaseController {

    private IAntiCounterfeitingUpdateContractService iAntiCounterfeitingUpdateContract;

    private ICheckUserPowerService iCheckUserPowerService;

    private IAntiCounterfeitingRegisteredOfficeService iAntiCounterfeitingRegisteredOfficeService;

    @Autowired
    public AntiCounterfeitingUpdateContractController(IAntiCounterfeitingUpdateContractService iAntiCounterfeitingUpdateContract, ICheckUserPowerService iCheckUserPowerService, IAntiCounterfeitingRegisteredOfficeService iAntiCounterfeitingRegisteredOfficeService) {
        this.iAntiCounterfeitingUpdateContract = iAntiCounterfeitingUpdateContract;
        this.iCheckUserPowerService = iCheckUserPowerService;
        this.iAntiCounterfeitingRegisteredOfficeService = iAntiCounterfeitingRegisteredOfficeService;
    }

    /**
     * 事务所申请修改联系人
     * @param antiCounterfeitingUpdateContract
     * @return
     */
    @Log(title = "事务所申请修改联系人", businessType = BusinessType.UPDATE)
    @PostMapping
    public AjaxResult applyUpdateContacts(@RequestBody @Validated AntiCounterfeitingUpdateContract antiCounterfeitingUpdateContract){
       return iAntiCounterfeitingUpdateContract.applyUpdateContacts(antiCounterfeitingUpdateContract);
    }

    /**
     * 查询联系人修改审批列表
     * @param antiCounterfeitingUpdateContract
     * @return*
     */
    @GetMapping("/list")
    public TableDataInfo list(AntiCounterfeitingUpdateContract antiCounterfeitingUpdateContract){
        if (iCheckUserPowerService.noAdminHashRole(RoleEnum.OFFICE_GROUPING_ADMIN.getRoleKey())) {
            List<String> officeIds = iAntiCounterfeitingRegisteredOfficeService.selectOfficeIdList();
            antiCounterfeitingUpdateContract.setOfficeIds(officeIds);
        }
        startPage();
        List<Map<String, Object>> list = iAntiCounterfeitingUpdateContract.list(antiCounterfeitingUpdateContract);
        return getDataTable(list);
    }

    /**
     * 审批通过-修改联系人
     * @param updateContract
     * @return
     */
    @Log(title = "审批通过-修改联系人", businessType = BusinessType.UPDATE)
    @PutMapping("/approved")
    public AjaxResult approved(@RequestBody AntiCounterfeitingUpdateContract updateContract){
        updateContract.setStatus(2);
        iAntiCounterfeitingUpdateContract.processApplications(updateContract);
        return AjaxResult.success();
    }

    /**
     * 审批拒绝-修改联系人
     * @param updateContract
     * @return
     */
    @Log(title = "审批拒绝-修改联系人", businessType = BusinessType.UPDATE)
    @PutMapping("/refuse")
    public AjaxResult refuse(@RequestBody AntiCounterfeitingUpdateContract updateContract){
        updateContract.setStatus(-2);
        iAntiCounterfeitingUpdateContract.processApplications(updateContract);
        return AjaxResult.success();
    }

    /**
     * 申请修改联系人导出
     * @param updateContract
     * @param response
     * @throws IOException
     */
    @Log(title = "申请修改联系人导出", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public void export(AntiCounterfeitingUpdateContract updateContract,HttpServletResponse response) throws IOException, InvalidFormatException {
        iAntiCounterfeitingUpdateContract.export(updateContract,response);
    }
}
